package com.cgs.mongo;

import com.cgs.mongo.engine.OperationalContext;
import com.cgs.mongo.engine.ScriptEngine;
import com.cgs.mongo.extractor.CursorExtractor;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

/**
 * 执行器
 */
public class MongoEngineActuator {

    /**
     * 执行器主函数
     *
     * @param collection MongoCollection 对象
     * @param sql        查询函数
     * @param extractor  结果提取器
     * @param <K>        提取出的对象类型
     * @return extractor提取出来的数
     */
    public static <K> K run(MongoCollection<Document> collection, String sql, CursorExtractor<K> extractor) {
        Object result = ScriptEngine.INSTANCE.run(new OperationalContext<>(collection, sql));
        return extractor.extractData(result);
    }
}
